/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bimager.jtwain;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import sun.net.www.URLConnection;

/**
 *
 * @author Amskape
 */
public class UploadUtility  {
    
   String lineEnd = "\r\n"; 
   String twoHyphens = "--"; 
   String boundary =  "*****"; 
   int bytesRead, bytesAvailable, bufferSize;
   byte[] buffer; 
   int maxBufferSize = 1*1024*1024;
   URL connectioUrl;
   HttpURLConnection connection;
   String Url;
   File uploadinFile;
   public UploadUtility(String url , File file) throws MalformedURLException, IOException {
   
        
       connectioUrl = new URL(url);
       connection = (HttpURLConnection) connectioUrl.openConnection();
       connection.setDoInput(true);
       connection.setDoOutput(true);
       
       connection.setUseCaches(false); 
       connection.setRequestMethod("POST");
       connection.setRequestProperty("Connection", "Keep-Alive"); 
       connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); 
       uploadinFile = file;
    
    }
    public boolean  doRequest() throws IOException 
    {
        
        DataOutputStream dos=null; 
        FileInputStream fileInputStream=null;
       try {
                 // Send a binary file
           dos = new DataOutputStream( connection.getOutputStream() );
           dos.writeBytes(twoHyphens + boundary + lineEnd); 
           dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + uploadinFile.getName() +"\"" + lineEnd); 
           dos.writeBytes(lineEnd); 
      
      

    // create a buffer of maximum size 

           fileInputStream = new FileInputStream(uploadinFile);
           bytesAvailable = fileInputStream.available(); 
           bufferSize = Math.min(bytesAvailable, maxBufferSize); 
           buffer = new byte[bufferSize]; 
           bytesRead = fileInputStream.read(buffer, 0, bufferSize); 

           while (bytesRead > 0) 
           { 
             dos.write(buffer, 0, bufferSize); 
             bytesAvailable = fileInputStream.available(); 
             bufferSize = Math.min(bytesAvailable, maxBufferSize); 
             bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
            } 
            dos.writeBytes(lineEnd); 
            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 
            fileInputStream.close(); 
            dos.flush(); 
            dos.close();
       } catch (IOException ex) {
           Logger.getLogger(UploadUtility.class.getName()).log(Level.SEVERE, null, ex);
       }
       finally
       {
           if(fileInputStream != null)
              fileInputStream.close(); 
           
           if(dos !=null){
            dos.flush(); 
            dos.close();  
           }
           
       }
        int status = connection.getResponseCode();
        PrintWriter  outputStream  = null;
      //  outputStream  = new PrintWriter(new FileWriter("error.html"));
        if (status == HttpURLConnection.HTTP_OK) {
            /*BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
             * String line = null;
             * while ((line = reader.readLine()) != null) {
             * outputStream.println(line);
             * 
             * }
             * reader.close();
             * outputStream.close();
             * connection.disconnect();*/
            return true;
            
		} else {
            return false;
			//throw new IOException("Server returned non-OK status: " + status);
		}      
    }
    
   
   
   
   
}
